#include "../header/common.h"

void isomorph(SDL_Rect obj,SDL_Surface *screen,SDL_Surface *objs,int *killeractive,int *barspeed,int *maxballs,int *forceeffect)
{
  /*FORCEEFFECT erzwingt einen Effekt,
    fuer den Client*/
  SDL_Surface *darks,*temp,*backup;
  SDL_Rect dark,ttfblend;
  TTF_Font *font;
  int shift,i,effect;
  srand (time(NULL));
  if(*forceeffect==0)
    {
      effect=(((int)14*(rand()/(RAND_MAX+1.0)))/2);
      if(effect<1)effect=7;
    }
  else effect=*forceeffect;  
  /*Effekte:
    1=Balken wird groesser
    2=Balken wird kleiner
    3=Balken wird zum Stachelbalken
    4=Balken wird zum Gummibaerchen
    5=Balken wird schneller
    6=Balken wird langsamer
    7=Zweiter Ball kommt hinzu
  */
  ttfblend.x=(screen->w/3);
  ttfblend.y=(screen->h/2);
  ttfblend.w=400;
  ttfblend.h=150;
  if(screen->w==640)
    {
      temp=SDL_LoadBMP("image/640480/temp.bmp");
      SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, 200);
      backup=SDL_LoadBMP("image/640480/temp.bmp");
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 40);
      darks = SDL_LoadBMP("image/640480/dark.bmp");
      SDL_SetColorKey(darks, SDL_SRCCOLORKEY, SDL_MapRGB(darks->format, 255, 255, 255)); 
      dark.w=(darks->w/10);
      dark.h=darks->h;
      dark.x=0;
      dark.y=0;
      switch(effect)
	{
	case 1 :
	  SDL_FreeSurface(objs);
	  objs = SDL_LoadBMP("image/640480/big.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 2 :
	  SDL_FreeSurface(objs);
	  objs = SDL_LoadBMP("image/640480/small.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 3 :
	  SDL_FreeSurface(objs);
	  if(obj.x<(screen->w/2))
	    {
	      objs = SDL_LoadBMP("image/640480/killer.bmp");
	      *killeractive=1;
	    }
	  else 
	    {
	      objs = SDL_LoadBMP("image/640480/killerr.bmp");
	      *killeractive=2;
	    }
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_BlitSurface(screen,NULL,backup,NULL);
	  for(i=0;i<3;i++)
	    {
	      SDL_BlitSurface(temp,NULL,screen,NULL);
	      SDL_Flip(screen);
	      SDL_Delay(200);
	      SDL_BlitSurface(backup,NULL,screen,NULL);
	      SDL_Flip(screen);
	      SDL_Delay(200);
	    }
	  break;
	case 4 :
	  SDL_FreeSurface(objs);
	  if(obj.x<(screen->w/2))objs = SDL_LoadBMP("image/640480/bearl.bmp");
	  else objs = SDL_LoadBMP("image/640480/bearr.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255));
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 5 :
	  drawText(screen,screen,font,"speeeed up!",screen->w/3,screen->h/2,255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(600);
	  SDL_FillRect(screen,&ttfblend,SDL_MapRGB(screen->format,255,255,255));  
	  SDL_Flip(screen);
	  *barspeed=30;
	  break;
	case 6 :
	  drawText(screen,screen,font,"slow down!",screen->w/3,screen->h/2,255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(600);
	  SDL_FillRect(screen,&ttfblend,SDL_MapRGB(screen->format,255,255,255));  
	  SDL_Flip(screen);
	  *barspeed=7;
	  break;
	case 7 :
	  *maxballs=2;
	  break;
	}
    }
  if(screen->w==800)
    {
      temp=SDL_LoadBMP("image/800600/temp.bmp");
      SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, 200);
      backup=SDL_LoadBMP("image/800600/temp.bmp");
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 45);
      darks = SDL_LoadBMP("image/800600/dark.bmp");
      SDL_SetColorKey(darks, SDL_SRCCOLORKEY, SDL_MapRGB(darks->format, 255, 255, 255)); 
      dark.w=(darks->w/10);
      dark.h=darks->h;
      dark.x=0;
      dark.y=0;
      switch(effect)
	{
	case 1 :
	  SDL_FreeSurface(objs);
	  objs = SDL_LoadBMP("image/800600/big.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255));
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 2 :
	  SDL_FreeSurface(objs);
	  objs = SDL_LoadBMP("image/800600/small.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 3 :
	  SDL_FreeSurface(objs);
	  if(obj.x<(screen->w/2))
	    {
	      objs = SDL_LoadBMP("image/800600/killer.bmp");
	      *killeractive=1;
	    }
	  else 
	    {
	      objs = SDL_LoadBMP("image/800600/killerr.bmp");
	      *killeractive=2;
	    }
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_BlitSurface(screen,NULL,backup,NULL);
	  for(i=0;i<3;i++)
	    {
	      SDL_BlitSurface(temp,NULL,screen,NULL);
	      SDL_Flip(screen);
	      SDL_Delay(200);
	      SDL_BlitSurface(backup,NULL,screen,NULL);
	      SDL_Flip(screen);
	      SDL_Delay(200);
	    }
	  break;
	case 4 :
	  SDL_FreeSurface(objs);
	  if(obj.x<(screen->w/2))objs = SDL_LoadBMP("image/800600/bearl.bmp");
	  else objs = SDL_LoadBMP("image/800600/bearr.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255));
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 5 :
	  drawText(screen,screen,font,"speeeed up!",screen->w/3,screen->h/2,255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(600);
	  SDL_FillRect(screen,&ttfblend,SDL_MapRGB(screen->format,255,255,255));  
	  SDL_Flip(screen);
	  *barspeed=46;
	  break;
	case 6 :
	  drawText(screen,screen,font,"slow down!",screen->w/3,screen->h/2,255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(600);
	  SDL_FillRect(screen,&ttfblend,SDL_MapRGB(screen->format,255,255,255));  
	  SDL_Flip(screen);
	  *barspeed=11;
	  break;
	case 7 :
	  *maxballs=2;
	  break;
	}
    }if(screen->w==1024)
    {
      temp=SDL_LoadBMP("image/1024768/temp.bmp");
      SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, 200);
      backup=SDL_LoadBMP("image/1024768/temp.bmp");
      font = TTF_OpenFont("spranq_eco_sans_regular.ttf", 50);
      darks = SDL_LoadBMP("image/1024768/dark.bmp");
      SDL_SetColorKey(darks, SDL_SRCCOLORKEY, SDL_MapRGB(darks->format, 255, 255, 255)); 
      dark.w=(darks->w/10);
      dark.h=darks->h;
      dark.x=0;
      dark.y=0;
      switch(effect)
	{
	case 1 :
	  SDL_FreeSurface(objs);
	  objs = SDL_LoadBMP("image/1024768/big.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255));
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 2 :
	  SDL_FreeSurface(objs);
	  objs = SDL_LoadBMP("image/1024768/small.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  break;
	case 3 :
	  SDL_FreeSurface(objs);
	  if(obj.x<(screen->w/2))
	    {
	      objs = SDL_LoadBMP("image/1024768/killer.bmp");
	      *killeractive=1;
	    }
	  else 
	    {
	      objs = SDL_LoadBMP("image/1024768/killerr.bmp");
	      *killeractive=2;
	    }
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_BlitSurface(screen,NULL,backup,NULL);
	  for(i=0;i<3;i++)
	    {
	      SDL_BlitSurface(temp,NULL,screen,NULL);
	      SDL_Flip(screen);
	      SDL_Delay(200);
	      SDL_BlitSurface(backup,NULL,screen,NULL);
	      SDL_Flip(screen);
	      SDL_Delay(200);
	    }
	  break;
	case 4 :
	  SDL_FreeSurface(objs);
	  if(obj.x<(screen->w/2))objs = SDL_LoadBMP("image/1024768/bearl.bmp");
	  else objs = SDL_LoadBMP("image/1024768/bearr.bmp");
	  for(shift=0;shift<(darks->w/dark.w);shift++)
	    {
	      SDL_BlitSurface(darks,&dark,screen,&obj);
	      SDL_Flip(screen);
	      SDL_Delay(50);
	      dark.x+=dark.w;
	    }  
	  SDL_FillRect(screen,&obj,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_BlitSurface(objs,NULL,screen,&obj);
	  SDL_Flip(screen);
	  SDL_BlitSurface(temp,NULL,screen,NULL); 
	  break;
	case 5 :
	  drawText(screen,screen,font,"speeeed up!",screen->w/3,screen->h/2,255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(600);
	  SDL_FillRect(screen,&ttfblend,SDL_MapRGB(screen->format,255,255,255));  
	  SDL_Flip(screen);
	  *barspeed=60;
	  break;
	case 6 :
	  drawText(screen,screen,font,"slow down!",screen->w/3,screen->h/2,255,0,0);
	  SDL_Flip(screen);
	  SDL_Delay(600);
	  SDL_FillRect(screen,&ttfblend,SDL_MapRGB(screen->format,255,255,255));  
	  SDL_Flip(screen);
	  *barspeed=15;
	  break;
	case 7 :
	  *maxballs=2;
	  break;
	}
    }
  SDL_FreeSurface(darks);
  SDL_FreeSurface(temp);
}

void special(int activate,SDL_Rect obj[],SDL_Surface *barls,SDL_Surface *barrs,SDL_Surface *screen,int *killeractive,int *barspeed,int *maxballs)
{
  SDL_Rect clear;
  int forceeffect=0;
  if(screen->w==640)
    {
      switch(activate)
	{
	case 0 : /*Lösche Special*/
 	  *barspeed=15; 
	  *killeractive=0;
	  
	  /*Linker Balken*/
	  clear.x=obj[0].x;clear.y=obj[0].y;
	  clear.w=barls->w;clear.h=barls->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barls);
	  barls = SDL_LoadBMP("image/640480/barl.bmp"); 
	  SDL_BlitSurface(barls,NULL,screen,&obj[0]);

	  /*Rechter Balken*/
	  clear.x=obj[1].x;clear.y=obj[1].y;
	  clear.w=barrs->w;clear.h=barrs->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barrs);
	  barrs = SDL_LoadBMP("image/640480/barr.bmp");
	  SDL_BlitSurface(barrs,NULL,screen,&obj[1]);
	  break;    
	case 1 : /*Linker Balken*/ 
	  isomorph(obj[0],screen,barls,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	case 2 : /*Rechter Balken*/
	  isomorph(obj[1],screen,barrs,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	}
    }

  if(screen->w==800)
    {
      switch(activate)
	{
	case 0 : /*Lösche Special*/
	  *barspeed=23;
	  *killeractive=0;

	  /*Linker Balken*/
	  clear.x=obj[0].x;clear.y=obj[0].y;
	  clear.w=barls->w;clear.h=barls->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barls);
	  barls = SDL_LoadBMP("image/800600/barl.bmp"); 
	  SDL_BlitSurface(barls,NULL,screen,&obj[0]);

	  /*Rechter Balken*/
	  clear.x=obj[1].x;clear.y=obj[1].y;
	  clear.w=barrs->w;clear.h=barrs->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barrs);
	  barrs = SDL_LoadBMP("image/800600/barr.bmp");
	  SDL_BlitSurface(barrs,NULL,screen,&obj[1]);
	  break;    
	case 1 : /*Linker Balken*/
	  isomorph(obj[0],screen,barls,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	case 2 : /*Rechter Balken*/
	  isomorph(obj[1],screen,barrs,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	}
    }

  if(screen->w==1024)
    {
      switch(activate)
	{
	case 0 : /*Lösche Special*/
 	  *barspeed=30; 
	  *killeractive=0; 	  

	  /*Linker Balken*/
	  clear.x=obj[0].x;clear.y=obj[0].y;
	  clear.w=barls->w;clear.h=barls->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barls);
	  barls = SDL_LoadBMP("image/1024768/barl.bmp"); 
	  SDL_BlitSurface(barls,NULL,screen,&obj[0]);

	  /*Rechter Balken*/
	  clear.x=obj[1].x;clear.y=obj[1].y;
	  clear.w=barrs->w;clear.h=barrs->h;
	  SDL_FillRect(screen,&clear,SDL_MapRGB(screen->format,255,255,255)); 
	  SDL_FreeSurface(barrs);
	  barrs = SDL_LoadBMP("image/1024768/barr.bmp");
	  SDL_BlitSurface(barrs,NULL,screen,&obj[1]);
	  break;    
	case 1 : /*Linker Balken*/
	  isomorph(obj[0],screen,barls,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	case 2 : /*Rechter Balken*/
	  isomorph(obj[1],screen,barrs,killeractive,barspeed,maxballs,&forceeffect);
	  break;
	}
    }
}

int randombox(SDL_Surface *screen,SDL_Rect box[],int *activebox,SDL_Rect obj[],SDL_Surface *barls,SDL_Surface *barrs, SDL_Surface *boxs, SDL_Rect field,int *killeractive,int *barspeed,int *maxballs)
{
  /*activebox=1 : Kommt von links,geht nach rechts
    activebox=2 : Kommt von rechts, geht nach links*/
      switch(*activebox)
	{
	case 1 :
	  if((box[0].x+box[0].w)<(obj[1].x+obj[1].w+10))
	    {
 	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255)); 
	      box[0].x+=(screen->w/100);
	      SDL_BlitSurface(boxs,NULL,screen,&box[0]);
	    }
	  else
	    {
	      if(((box[0].x+box[0].w)>=obj[1].x)&&(((box[0].y>obj[1].y)&&(box[0].y<(obj[1].y+obj[1].h)))||((box[0].y<obj[1].y)&&((box[0].y+box[0].h)>obj[1].y))))
		{
		  SDL_FillRect(screen,&obj[1],SDL_MapRGB(screen->format,255,255,255)); 
		  special(2,obj,barls,barrs,screen,killeractive,barspeed,maxballs);
		}
	      *activebox=2;
 	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
 	      SDL_BlitSurface(barrs,NULL,screen,&obj[1]);
	      box[0].x=(obj[1].x-box[0].w-(barrs->w));
	      box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
	      if(box[0].y<field.y)box[0].y+=field.y;
	    }
	  return 0;
	case 2 :
	  if(box[0].x>(obj[0].x-10))
	    {
	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
 	      box[0].x-=(screen->w/100);
	      SDL_BlitSurface(boxs,NULL,screen,&box[0]);
	    }
	  else
	    {
	      if((box[0].x<=(obj[0].x+obj[0].w))&&(((box[0].y>obj[0].y)&&(box[0].y<(obj[0].y+obj[0].h)))||((box[0].y<obj[0].y)&&((box[0].y+box[0].h)>obj[0].y))))
		{
		  SDL_FillRect(screen,&obj[0],SDL_MapRGB(screen->format,255,255,255)); 
		  special(1,obj,barls,barrs,screen,killeractive,barspeed,maxballs);
		}
	      *activebox=1;
	      SDL_FillRect(screen,&box[0],SDL_MapRGB(screen->format,255,255,255));
 	      SDL_BlitSurface(barls,NULL,screen,&obj[0]);
	      box[0].x=(obj[0].x+obj[0].w+(barls->w));
	      box[0].y=((int)(field.y+field.h-box[0].h)*(rand()/(RAND_MAX+1.0)));
	      if(box[0].y<field.y)box[0].y+=field.y;
	    }
	  return 0;
	}
  return 0;
}
